package com.dunqi.gpm.shunqing.home.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.activity.ProjectProgressDetailsActivity;
import com.dunqi.gpm.shunqing.home.adapter.ProjectTimelineAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class ProjectTimelineFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    private ProjectTimelineAdapter adapter;
    List<ProjectTimeline> data = new ArrayList<>();
    private Project project;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectProgressList(project == null ? null : project.getProjectId(), page)
                .compose(RxSchedulers.<Page<ProjectTimeline>>io_main())
                .subscribe(new Consumer<Page<ProjectTimeline>>() {
                    @Override
                    public void accept(Page<ProjectTimeline> projectTimelinePage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<ProjectTimeline> list = projectTimelinePage.getList();

                        if (page == 1) {
                            data.clear();
                        }

//                        for (int i = 0; i < list.size(); i++) {
//                            ProjectTimeline item = list.get(i);
//                            int progressType = item.getProgressType();
//                            if (progressType == 0) {
//                                list.remove(i);
//                                i--;
//                            }
//                        }

                        data.addAll(list);

                        adapter.notifyDataSetChanged();

                        if (projectTimelinePage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }

                        page++;
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new ProjectTimelineAdapter(context, data);
        recycler.setAdapter(adapter);

        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);
        adapter.setEmptyView(new EmptyView(context));

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_time_line;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        project = (Project) arguments.getSerializable(Constance.PROJECT);
    }

    public static ProjectTimelineFragment newInstance(Project project) {
        ProjectTimelineFragment fragment = new ProjectTimelineFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constance.PROJECT, project);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectProgressDetailsActivity.start((Activity) context, data.get(position).getProPlanProgressId());
    }
}
